﻿using System;
using Glue;
using Xunit;

namespace GlueTests.MapperTests
{
    public class When_mapping_a_nested_onedirectional_relation
    {
        private readonly Mapping<DomainPerson, GuiPerson> mapping;
        private readonly Mapping<DomainAddress, GuiAddress> addressMapping;

        public class GuiPerson { public GuiAddress Address { get; set; } }

        public class GuiAddress { public String City { get; set; } }

        public class DomainPerson { public DomainAddress Address { get; set; } }

        public class DomainAddress { public String City { get; set; } }

        public When_mapping_a_nested_onedirectional_relation()
        {
            mapping = new Mapping<DomainPerson, GuiPerson>();
            addressMapping = new Mapping<DomainAddress, GuiAddress>();
            addressMapping.Relate(domain => domain.City, gui => gui.City);
        }

        [Fact]
        public void Should_not_map_TowardsRight_When_mapping_is_TowardsLeft()
        {
            mapping.RelateTowardsLeft(domain => domain.Address, gui => gui.Address, addressMapping);
            var cityName = "CityNameFromDomainAddress";

            var domainPerson = GetDomainPerson(cityName);
            var guiPerson = mapping.Map(domainPerson, GetGuiPerson(""));
            Assert.Equal("",guiPerson.Address.City);
        }

        [Fact]
        public void Should_map_TowardsLeft_When_mapping_is_TowardsLeft()
        {
            mapping.RelateTowardsLeft(domain => domain.Address, gui => gui.Address, addressMapping);
            var cityName = "CityNameFromGuiAddress";
            
            var guiPerson = GetGuiPerson(cityName);
            var domainPerson = mapping.Map(guiPerson, GetDomainPerson(""));
            Assert.Equal(cityName, domainPerson.Address.City);
        }

        [Fact]
        public void Should_not_map_TowardsLeft_When_mapping_is_TowardsRight()
        {
            mapping.RelateTowardsRight(domain => domain.Address, gui => gui.Address, addressMapping);
            var cityName = "CityNameFromGuiAddress";

            var guiPerson = GetGuiPerson(cityName);
            var domainPerson = mapping.Map(guiPerson, GetDomainPerson(""));
            Assert.Equal("", domainPerson.Address.City);   
        }

        [Fact]
        public void Should_map_TowardsRight_When_mapping_is_TowardsRight()
        {
            mapping.RelateTowardsRight(domain => domain.Address, gui => gui.Address, addressMapping);
            var cityName = "CityNameFromDomainAddress";

            var domainPerson = GetDomainPerson(cityName);
            var guiPerson = mapping.Map(domainPerson, GetGuiPerson(""));
            Assert.Equal(domainPerson.Address.City, guiPerson.Address.City);
        }

        private GuiPerson GetGuiPerson(string cityName)
        {
            return new GuiPerson { Address = new GuiAddress { City = cityName } };
        }

        private DomainPerson GetDomainPerson(string cityName)
        {
            return new DomainPerson {Address = new DomainAddress {City = cityName}};
        }
    }
}
